const express = require('express');
const router = express.Router();
const md5 = require('md5');
const managerModel = require('../../model/manager.js')
router.get('/',(req,res)=>{

    res.render('admin/manager/wellcom')
})
router.get('/list',async (req,res)=>{
   let list= await managerModel.find({})
   res.render('admin/manager/managerList',{list})
})
router.get('/add',(req,res)=>{
    res.render('admin/manager/add')
    // res.send('你好')
})
router.post('/doadd', async (req,res)=>{
    let username = req.body.username;
    let password = req.body.password;
    let rpassword = req.body.rpassword;
    let phone = req.body.phone;
    let email = req.body.email;
    let status = req.body.status;
    // console.log(req.app.locals)
    if(username===''){
        res.render('admin/status',{
            redirectUrl:'/admin/manager/add',
            title:'账号不能为空',
            message:'原页面'
        })
        return;
    }
    if(password.length<3){
        res.render('admin/status',{
            redirectUrl:'/admin/manager/add',
            title:'密码不能小于6位',
            message:'原页面'
        })
        return;
    }
    if(password!= rpassword){
        res.render('admin/status',{
            redirectUrl:'/admin/manager/add',
            title:'密码不一样',
            message:'原页面'
        })
        return;
    }

  let result= await managerModel.find({username})
  if(result.length>0){
      res.render('admin/status',{
          redirectUrl:'/admin/manager/add',
          title:'账号已经存在,请换一个',
          message:'原页面'
      })
      return;
  }else{
    let manager = new managerModel(
     {
        username,
         password:md5(password),
         phone,
         email,
         status,
         add_time:Date.now()
     }
    )
   await manager.save()
    res.redirect('/admin/manager/list')
  }

})
router.get('/edit',async (req,res)=>{
    let id = req.query.id;
    let user = await managerModel.find({_id:id})
  
    res.render('admin/manager/edit',user[0])
})
router.post('/doEdit',async (req,res)=>{
    let {id,password,rpassword,phone,email,status} = req.body;
    if(password.length>0){
        if(password.length<3){
            res.render('admin/status',{
                redirectUrl:'/admin/manager/edit?id='+id,
                title:'密码不能小于3位',
                message:'原页面'
            })
            return;
        }
        if(password!=rpassword){
            res.render('admin/status',{
                redirectUrl:'/admin/manager/edit?id='+id,
                title:'确认密码有误',
                message:'原页面'
            })
            return;
        }
       await managerModel.updateOne({_id:id},{
            password:md5(password),
            phone,
            email,
            status
        })

    }else{
       await managerModel.updateOne({_id:id},{
            phone,
            email,
            status
        })
    }
    res.redirect('/admin/manager/list')
})
router.get('/delete',async (req,res)=>{
    let id = req.query.id;
    let result = await managerModel.deleteOne({_id:id})
    res.render('admin/status',{
        redirectUrl:'/admin/manager/list',
        title:'删除成功',
        message:'列表页面'
    })
})
module.exports = router;